package com.sanjiahantang.youjiarole.module.common.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyGridView;
import com.sanjiahantang.uilibrary.views.bannerview.BannerView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.common.beans.HomeBean;
import com.sanjiahantang.youjiarole.module.common.holders.ClassifyHolder;
import com.sanjiahantang.youjiarole.module.common.holders.CommonHolder;
import com.sanjiahantang.youjiarole.module.common.holders.ComprehensiveHolder;
import com.sanjiahantang.youjiarole.module.common.holders.MyBaseViewHolder;
import com.sanjiahantang.youjiarole.module.common.holders.ShopHolder;
import com.sanjiahantang.youjiarole.module.common.views.impl.MainActivity;
import com.sanjiahantang.youjiarole.module.common.views.impl.WebActivity;
import com.sanjiahantang.youjiarole.utils.FileUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/6/8.
 */

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StaticValue {

    private final Context context;
    //布局标识集合
    private final List<Integer> typeList;
    private LayoutInflater inflater;

    private HomeBean.Data data;
    private MainActivity activity;

    //各种类型
    public static final int TYPE_BANNER = 1;//轮播图
    public static final int TYPE_CLASSIFY = 2;//功能入口列表
    public static final int TYPE_CASE = 3;//案例
    public static final int TYPE_NEARSTORE=4;//附近体验店
    public static final int TYPE_MATCH_TITLE = 5;//搭配标题
    public static final int TYPE_MATCH = 6;//搭配

    private static int counter = 0;
    private HashMap<Integer,Integer> collectionMap;

    public HomeAdapter(Context context) {
        this.context = context;
        this.typeList = new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
    }

    public HomeAdapter(Context context, HomeBean.Data data, MainActivity activity) {
        this(context);
        this.data = data;
        this.activity = activity;
        typeList.add(TYPE_BANNER);
        typeList.add(HOLDER_TYPE_TAKE_ORDER);
        typeList.add(TYPE_CLASSIFY);
        typeList.add(TYPE_CASE);
        typeList.add(TYPE_MATCH_TITLE);
        collectionMap = new HashMap<>();
        if(data != null && data.getMatches() != null){
            for (int i = 0,max = data.getMatches().size(); i < max; i++){
                typeList.add(TYPE_MATCH);
                collectionMap.put(i + 5,i);//跳过的个数 只在这里写一次
            }
        }
        typeList.add(HOLDER_TYPE_ARTICLE);

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {

        switch (typeList.get(position)) {
            case TYPE_BANNER:   //轮播图和输入框
                BannerView bv = new BannerView(activity);
                bv.setHeight(200);
                bv.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, Utils.dip2px(activity, 200)));

                List<HashMap<String,Object>> imgList = new ArrayList<>();
                for (final HomeBean.Advertise url:data.getAdvertisementList()) {
                    HashMap<String,Object> map = new HashMap<>();
                    map.put("img", FileUtils.rePath(url.getUrl()));
                    map.put("action", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra("url",url.getLinkLocation());
                            context.startActivity(intent);
                        }
                    });
                    imgList.add(map);
                }
                bv.initAdapterData(imgList);
                bv.setDrawerMode(BannerView.STYLE_Dot_Rect);
                bv.setLocationMode(BannerView.MODE_RIGHT);

                return new ShopHolder((Activity) context,bv,TYPE_BANNER,null);
            case TYPE_CLASSIFY: //功能菜单
                return new ClassifyHolder(inflater.inflate(R.layout.item_holder_classify, null),
                        context, activity);

            case TYPE_CASE:    //案例
                return new ComprehensiveHolder(inflater.inflate(R.layout.item_comprehensive, null),
                        TYPE_CASE,
                        context, data.getCases(),"案例","Case");
            case TYPE_NEARSTORE:   //附近体验店
                return new ComprehensiveHolder(inflater.inflate(R.layout.item_comprehensive, null),
                        TYPE_NEARSTORE,
                        context, data.getStores(),"附近体验店","Experience StoreBean");
            case TYPE_MATCH_TITLE:    //搭配标题
                return new ComprehensiveHolder(inflater.inflate(R.layout.item_comprehensive, null),
                        TYPE_MATCH_TITLE,
                        context, null,"搭配","Collocation");
            case TYPE_MATCH:    //搭配
                return new ShopHolder(activity,inflater.inflate(R.layout.item_match, null),
                        ShopAdapter.TYPE_COLLOCATION,data.getMatches().get(collectionMap.get(position)));
            case HOLDER_TYPE_ARTICLE:
                return new CommonHolder(activity,inflater.inflate(R.layout.item_comprehensive, null),
                        HOLDER_TYPE_ARTICLE,data.getArticleList());
            case HOLDER_TYPE_TAKE_ORDER:
                MyGridView gridView = new MyGridView(activity);
                gridView.setGravity(Gravity.CENTER_HORIZONTAL);
                gridView.setNumColumns(3);
                return new CommonHolder(activity,gridView,HOLDER_TYPE_TAKE_ORDER,data.getOrderTakingList());

        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyBaseViewHolder) {
            ((MyBaseViewHolder) holder).bindData();
        }
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }


}
